package hibernate.modelConverter;

import hibernate.model.Aygityazilimlari;
import hibernate.service.EmmyService;

import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.convert.Converter;
import javax.faces.convert.FacesConverter;

import org.springframework.context.support.ClassPathXmlApplicationContext;

@FacesConverter("AygitYazilimConverter")
public class AygitYazilimConverter implements Converter{

	ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("application-context.xml");
	EmmyService emmyService = (EmmyService) context.getBean("modelServices");
	
	@Override
	public Object getAsObject(FacesContext fc, UIComponent uic, String value) {
			System.out.println("yazilim-3");
			//AygitBean agtBean = (AygitBean) fc.getExternalContext().getSessionMap().get("AygitBean");
			if(!value.equals("")){
				System.out.println("value ="+ value);
				
				Aygityazilimlari ayYzl = (Aygityazilimlari) emmyService.findById(new Aygityazilimlari(), 
						Long.parseLong(value.trim()));
				System.out.println("malibu : "+ayYzl.getAygitYazid());
				System.out.println("malibu : "+ayYzl.getAciklama());
				return ayYzl;
			}
						
			System.out.println("yazilim-4");
			return null; // **** alternative with return null; ****
	}
	
	@Override
	public String getAsString(FacesContext fc, UIComponent uic, Object value) {
		if (value == null || value.equals("")) {
			System.out.println("yazilim-1");
			return "";
		} else {
			Aygityazilimlari ayYzl = (Aygityazilimlari) value;
			System.out.println("yazilim-2 ve id " + ayYzl.getAygitYazid());
			System.out.println("yazilim-2 ve acklm " + ayYzl.getAciklama());	
			return ""+ayYzl.getAygitYazid();
		}
	}
}